- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathStringsDemo.java
46 lines (35 loc) Β· 1.25 KB
/
StringsDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
packageLecture7;
publicclassStringsDemo {
publicstaticvoidmain(String[] args) {
Stringstr = "Hello";
System.out.println(str); // Hello
System.out.println(str.charAt(0)); // H
System.out.println(str.charAt(1)); // e
System.out.println(str.length()); // 5
System.out.println(str.charAt(str.length() - 1)); // o
System.out.println(str.substring(2)); // llo
System.out.println(str.substring(1, 3)); // el
System.out.println(str.substring(2, 2)); // blank space
System.out.println("i am executed");
System.out.println(str.startsWith("Hel")); // true
System.out.println(str.startsWith("hel")); // false
System.out.println(str.startsWith("HE")); // false
Strings1 = "hello";
Strings2 = "hello";
Strings3 = s1;
Strings4 = newString("hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // true
System.out.println(s2 == s3); // true
System.out.println(s1 == s4); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
System.out.println(s1.equals(s4)); // true
/*
* == checks for the address, here s1, s2, s3 are pointing to same
* address in string pool. s4 is created outside of string pool
*
* .equals() checks for the content
*/
}
}